﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess_Layer;

public partial class Download : System.Web.UI.Page
{
    string loginedUserName;

    /// <summary>
    /// Cho phép download bằng cách gọi trang 'Download.aspx?ProdID=Prod01'
    /// Lưu ý: 
    /// +User phải đăng nhập mới được phép download file.
    /// +Ebook luôn có dạng pdf
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        object currentUser = Session["ThanhVien"];
        if (currentUser != null)
        {
            loginedUserName = Helper.getCurrentUserName((DataTable)currentUser);
            string productID = Request.Params["ProdID"];
            if (Order_DAC.checkEbookOwnBy(productID, loginedUserName))
            {
                Response.ContentType = "Application/pdf";
                string FilePath = MapPath(getFilePath(int.Parse(productID)));
                Response.WriteFile(FilePath);
                Response.End();
            }
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }

    private string getFilePath(long productID)
    {
        return Product_DAC.getEbookDownloadLink(productID);
    }
}